package rps.client.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import rps.client.UIController;

public class Menu {

	private final JFrame frame;
	private final UIController controller;

	private final JMenuBar menuBar = new JMenuBar();
	private final JMenu menuGame = new JMenu("Spiel");
	private final JMenuItem menuGameNew = new JMenuItem("Neu");
	private final JMenuItem menuGameSurrender = new JMenuItem("Aufgeben");
	private final JMenuItem menuGameExit = new JMenuItem("Beenden");
	private final JCheckBoxMenuItem playMusic = new JCheckBoxMenuItem("Musik an", true);
	private final JMenu menuAbout = new JMenu("Über");
	private final JMenuItem credits = new JMenuItem("Credits");

	public Menu(JFrame frame, UIController controller) {

		this.frame = frame;
		this.controller = controller;

		buildMenuStructure();
		bindMenuActions();
	}

	private void buildMenuStructure() {
		menuGame.setMnemonic(KeyEvent.VK_S);
		menuBar.add(menuGame);
		menuGameNew.setMnemonic(KeyEvent.VK_N);
		menuGame.add(menuGameNew);
		menuGameSurrender.setMnemonic(KeyEvent.VK_A);
		menuGame.add(menuGameSurrender);
		menuGame.addSeparator();
		playMusic.setMnemonic(KeyEvent.VK_M);
		menuGame.add(playMusic);
		menuGame.addSeparator();
		menuGame.add(menuGameExit);
		
		menuBar.add(menuAbout);
		menuAbout.add(credits);
		
		frame.setJMenuBar(menuBar);

		menuGameSurrender.setEnabled(false);
		menuGameNew.setEnabled(false);
	}

	private void bindMenuActions() {
		menuGameNew.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				controller.handleNewGame();

			}
		});
		menuGameSurrender.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				controller.handleSurrender();

			}
		});
		playMusic.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(MediaProvider.getMediaProvider().isPlayingMusic()){
					playMusic.setSelected(false);
					MediaProvider.getMediaProvider().playMusic(false);
				}
				else{
					playMusic.setSelected(true);
					MediaProvider.getMediaProvider().playMusic(true);
				}				
			}
		});
		menuGameExit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				controller.handleExit();
			}
		});		
		credits.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
					@SuppressWarnings("unused")
					AboutPane ab = new AboutPane(new JFrame());				
			}
		});
	}

	public void gameStarted() {
		menuGameNew.setEnabled(true);
		menuGameSurrender.setEnabled(true);
	}

	public void gameEnded() {
		menuGameNew.setEnabled(true);
		menuGameSurrender.setEnabled(false);
	}

	public void reset() {
		menuGameNew.setEnabled(false);
		menuGameSurrender.setEnabled(false);
	}
}